package com.a4yy.web;

import com.a4yy.ReloveListBean;
import com.base.util.HttpClient;
import com.common.BaseController;
import com.common.util.a4yy.IRelove;
import com.common.util.a4yy.impl.ReloveImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.ByteArrayOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/a4yy")
public class A4yyMovieController extends BaseController{

    private final static IRelove relove = new ReloveImpl();

    @RequestMapping("/to/{view}")
    public ModelAndView toView(@PathVariable("view") String viewPath,String param){
        ModelAndView view = new ModelAndView();
        view.setViewName("a4yy/"+viewPath);
        view.addObject("param",param);
        return view;
    }


    /**
     * 获取某一个详情
     * @param url
     * @return
     */
    @RequestMapping("/getDetail")
    @ResponseBody
    public Object getByUrl(String url) throws Exception{
        ReloveListBean bean=null;
        if(url.contains("vod-detail-id-")){
            String content = downloadHtml(url);
            bean = relove.getDetail(content);
        }else if(url.contains("vod-list-id-")){
            String content = downloadHtml(url);
            content = downloadHtml(url);
            List list = relove.getSearch(content);
            return list;
        }else if(url.contains("vod-search-pg-")){
            int index=-1;
            String key="vod-search-pg-";
            if((index=url.indexOf("vod-search-pg-"))>-1){
                url=url.substring(0,index+key.length())+ URLEncoder.encode(URLDecoder.decode(url.substring(index+key.length()),"UTF-8"),"UTF-8");
                String content = downloadHtml(url);
                List list = relove.getSearch(content);
                return list;
            }
        }else{
            String content = downloadHtml(url);
            Document document = Jsoup.parse(content);
            String realUrl = document.select("iframe").get(0).attr("src");
            return getByUrl(realUrl);
        }
        return bean;
    }

    /**
     * 搜索结果获取
     * @param key
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public Object search(String key,String page) throws Exception{
        key=key==null?"":key;
        page=page==null?"1":page;
        String url="http://aaxxy.com/vod-search-pg-"+page+"-wd-"+ URLEncoder.encode(key,"UTF-8")+".html";
        String content = downloadHtml(url);
        List<ReloveListBean> list = relove.getSearch(content);
        return list;
    }


    private static String downloadHtml(String url) throws Exception{
        HttpClient.addHead(HttpClient.HEAD_USER_AGENT,HttpClient.AGENT_CHROME_HEAD);
        ByteArrayOutputStream baos=null;
        try{
            baos = new ByteArrayOutputStream();
            HttpClient.request(url,null,baos);
            String str = new String(baos.toByteArray(),"UTF-8");
            HttpClient.clearAll();
            return str;
        }catch (Exception e){
            throw e;
        }finally {
            try{
                baos.close();
            }catch (Exception e){}
        }
    }
}
